import { onMounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { getCategoryApi } from '@/apis/category'
export function useCategory() {
  const route = useRoute();
  const categoryList = ref([]);
  // 获取分类列表
  const getCategoryList = async () => {
    const res = await getCategoryApi(route.params.id);
    categoryList.value = res.result;
  };
  // dom加载完成后获取数据
  onMounted(() => getCategoryList());
  // 监听路由参数变化，如果发生变化则重新获取数据
  watch(
    () => route.params.id,
    () => getCategoryList()
  );
  return {
    categoryList
  }
}
